import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { getUserFromRequest } from '@/lib/auth'

export async function DELETE(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const user = getUserFromRequest(request)
    
    if (!user) {
      return NextResponse.json(
        { error: '未授权访问' },
        { status: 401 }
      )
    }

    const videoId = parseInt(params.id)
    
    if (isNaN(videoId)) {
      return NextResponse.json(
        { error: '无效的视频ID' },
        { status: 400 }
      )
    }

    // 检查视频是否存在
    const video = await prisma.tikTokVideo.findUnique({
      where: { id: videoId }
    })

    if (!video) {
      return NextResponse.json(
        { error: '视频不存在' },
        { status: 404 }
      )
    }

    // 删除视频（级联删除相关的文案提取记录）
    await prisma.tikTokVideo.delete({
      where: { id: videoId }
    })

    return NextResponse.json({
      message: '视频删除成功'
    })

  } catch (error) {
    console.error('Delete video error:', error)
    return NextResponse.json(
      { error: '删除视频失败' },
      { status: 500 }
    )
  }
}
